/**
 * Created by zhanglihe on 15/10/30.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog'
], function( $, common, ready, template, Collection, Model, dialog ) {

    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            var _this = this;
            loading();
            this.event();
            this.fetch();
        },

        fetch: function(_args) {
            var args = _args || {};
            args.limit = 10;
            args.offset = 0;
            var _this = this;
            this.collection = new Collection({
                container: 'data-list',
                template: 'data-item',
                url: _global.url.api + 'comments',
                condition: args,
                model: {
                    tag_name: 'tr',
                    url: {
                        put: _global.url.api + 'comment/{id}'
                    },
                    event: {
                        '.item-online:click': 'online',
                        '.item-offline:click': 'offline',
                        '.item-sz:click': 'lookImgs'
                    },
                    online: function(e) {
                        e.preventDefault();
                        var _this = this;
                        $.ajax({
                            url: _global.url.api + 'comment/' + _this.data.id,
                            type: 'put',
                            data: {status:1},
                            success: function(res) {
                                if(res.code == 0) {
                                    _this.data.status = 1;
                                    _this.create_element();
                                }
                            },
                            error: function() {
                                Alert({msg: '通讯错误', type: 'error'});
                            }
                        });
                    },
                    offline: function(e) {
                        e.preventDefault();
                        var _this = this;
                        $.ajax({
                            url: _global.url.api + 'comment/' + _this.data.id,
                            type: 'put',
                            data: {status:0},
                            success: function(res) {
                                if(res.code == 0) {
                                    _this.data.status = 0;
                                    _this.create_element();
                                }
                            },
                            error: function() {
                                Alert({msg: '通讯错误', type: 'error'});
                            }
                        });
                    },
                    lookImgs: function(e) {
                        e.preventDefault();
                        var _this = this;
                        var images = _this.data.images;

                        dialog({
                            content: template('imgs-tpl', {images: images}),
                            ok: function() {

                            }, cancel: function() {}
                        }).show();
                    },
                    change: function(e) {
                        this.data[$(e.target).attr('name')] = $(e.target).val();
                    }
                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
            this.showCategory();
        },

        event: function() {
            var _this = this;

            /**
             * 搜索事件
             */
            $('#btn-search').on('click', function(e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.reload();
            });

            /**
             * 按状态查询
             */
            $('#btn-status').find('a').on('click', function(e) {
                e.preventDefault();
                _this.collection.condition.keyword = '';
                _this.collection.condition.status = $(e.target).attr('rel');
                _this.collection.reload();
                $(e.target).addClass('active').siblings().removeClass('active');
            });
        },

        change: function(e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }
    };

    new index();
});